package com.example.materialviewpagerdemo;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

import com.example.materialviewpagerdemo.bean.ManualMoudel;
import com.example.materialviewpagerdemo.util.FyyDialog;
import com.example.materialviewpagerdemo.util.Sendcommand;
import com.github.florent37.materialviewpager.header.MaterialViewPagerHeaderDecorator;
import com.zyao89.view.zloading.ZLoadingDialog;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;



public class TestFragment1 extends Fragment {
    public static TestFragment1 newInstance() {
        return new TestFragment1();
    }
    private Button databutton;
    private Button supstartbutton;
    private Button supstopbutton;
    private Button prostartbutton;
    private Button prostopbutton;
    private long startTim = 0;//定义上一次单击的时间
    private long curTime = 0;//定义上第二次单击的时间
    private SharedPreferences sharedPreferences;
    private SharedPreferences.Editor editor;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_manual,container,false);
        //databutton=(Button)view.findViewById(R.id.button1);
        supstartbutton=(Button)view.findViewById(R.id.button2);
        supstopbutton=(Button)view.findViewById(R.id.button3);
        prostartbutton=(Button)view.findViewById(R.id.button4);
        prostopbutton=(Button)view.findViewById(R.id.button5);
        sharedPreferences = getActivity().getSharedPreferences("userInfo", Activity.MODE_PRIVATE);//fragment中获取SharedPreferences需要多加一步getActivity()方法
        editor = sharedPreferences.edit();

        /*databutton.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                String command = null;
                command = "BB00nanxiaoy003EF";
                new Thread(new Sendcommand(command)).start();
                Toast.makeText(TestFragment1.this.getActivity(),"指令发送成功!",Toast.LENGTH_LONG).show();
                //Toast.makeText(getActivity(),"发送成功",Toast.LENGTH_SHORT).show();
            }

        });*/
        supstartbutton.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                String command = null;
                command = "BB00:shouguangy2:EF";
                new Thread(new Sendcommand(command)).start();
                Toast.makeText(TestFragment1.this.getActivity(),"指令发送成功!",Toast.LENGTH_LONG).show();
                //Toast.makeText(getActivity(),"a成功",Toast.LENGTH_SHORT).show();
            }

        });
        supstopbutton.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                String command = null;
                command = "BB00:shouguangy2:01";
                new Thread(new Sendcommand(command)).start();
                Toast.makeText(TestFragment1.this.getActivity(),"指令发送成功!",Toast.LENGTH_LONG).show();
                //Toast.makeText(getActivity(),"B成功",Toast.LENGTH_SHORT).show();
            }

        });
       prostartbutton.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                //sharedPreferences= PreferenceManager.getDefaultSharedPreferences(this);
                curTime=sharedPreferences.getLong("time",0);
                if (curTime!=0)
                    startTim=(new Date()).getTime();


                    Log.d("onclik","点击的时间"+(startTim-curTime));
                if(startTim==0) Toast.makeText(TestFragment1.this.getActivity(),"发送成功!",Toast.LENGTH_LONG).show();

                else  if ((startTim-curTime)>30000){

                        Toast.makeText(TestFragment1.this.getActivity(),"时间发送成功!",Toast.LENGTH_LONG).show();
                }
                    else Toast.makeText(TestFragment1.this.getActivity(),"关闭时间未超过30分钟，发送失败!",Toast.LENGTH_LONG).show();
               // publishOrder();//发布方法
               /* String command = null;
                command = "BB00nanxiaokz0311";
                new Thread(new Sendcommand(command)).start();
                Toast.makeText(TestFragment1.this.getActivity(),"指令发送成功!",Toast.LENGTH_LONG).show();
                //Toast.makeText(getActivity(),"成功",Toast.LENGTH_SHORT).show();*/
            }

        });
        prostopbutton.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){


                    curTime=(new Date()).getTime();//关闭单击的时间
                    editor.putLong("time",curTime);
                    editor.commit();

                /*String command = null;
                command = "BB00nanxiaokz0312";
                new Thread(new Sendcommand(command)).start();
                Toast.makeText(TestFragment1.this.getActivity(),"指令发送成功!",Toast.LENGTH_LONG).show();*/
               Toast.makeText(getActivity(),"成功",Toast.LENGTH_SHORT).show();
            }

        });




        return view;
    }



}
